// import { getCommentDetail } from '../../../../services/good/comments/fetchCommentDetail';
import Toast from 'tdesign-miniprogram/toast/index';
import requestApi from "../../../../../utils/request"
import http from "../../../../../utils/http"

Page({
  data: {
    serviceRateValue: 1,
    goodRateValue: 1,
    conveyRateValue: 1,
    isAnonymous: false,
    uploadFiles: [],
    gridConfig: {
      width: 218,
      height: 218,
      column: 3,
    },
    isAllowedSubmit: false,
    imgUrl: '',
    title: '',
    goodsDetail: '',
    imageProps: {
      mode: 'aspectFit',
    },
    fileList: []
  },

  onLoad(options) {
    const orderItem = JSON.parse(wx.getStorageSync('order.orderItem'))
    console.log(orderItem)
    this.setData({
      imgUrl: orderItem.imgUrl,
      title: orderItem.title,
      goodsDetail: orderItem,
    });
  },

  onRateChange(e) {
    const { detail } = e;
    const item = e?.currentTarget?.dataset?.item;
    this.setData({ [item]: detail }, () => {
      this.updateButtonStatus();
    });
  },

  onAnonymousChange(e) {
    const status = !!e?.detail?.checked;
    this.setData({ isAnonymous: status });
  },

  handleSuccess(e) {
    const { files } = e.detail;

    this.setData({
      uploadFiles: files,
    });
  },

  afterRead(event) {
    const { fileList } = this.data
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    http.upload(file.url, {
      type: 'image'
    }).then(res => {
      const newList = []
      newList.push({
        url: res.data.fullurl,
        pathUrl: res.data.url
      })
      if (fileList.length > 0) {
        fileList.forEach(e => {
          newList.push(e)
        })
      }
      this.setData({
        fileList: newList
      })
    })
  },
  handleRemove(e) {
    const { index } = e.detail;
    const { uploadFiles } = this.data;
    uploadFiles.splice(index, 1);
    this.setData({
      uploadFiles,
    });
  },

  onTextAreaChange(e) {
    const value = e?.detail?.value;
    this.textAreaValue = value;
    this.updateButtonStatus();
  },

  updateButtonStatus() {
    const { serviceRateValue, goodRateValue, conveyRateValue, isAllowedSubmit } = this.data;
    const { textAreaValue } = this;
    const temp = serviceRateValue && goodRateValue && conveyRateValue && textAreaValue;
    if (temp !== isAllowedSubmit) this.setData({ isAllowedSubmit: temp });
  },

  onSubmitBtnClick() {
    const { isAllowedSubmit, goodRateValue, goodsDetail } = this.data;
    if (!isAllowedSubmit) return;
    const { fileList } = this.data

    const urlArr = []
    if (fileList.length > 0) {
      fileList.forEach(e => {
        urlArr.push(e.pathUrl)
      })
    }
    const imgStr = urlArr.length > 0 ? urlArr.join(',') : ''
    const params = {
      content: isAllowedSubmit,
      level: goodRateValue,
      order_item_id: goodsDetail.orderItemId,
      id: goodsDetail.id,
      images: imgStr
    }
    console.log(params)
    requestApi.post('shopro/order/comment', params).then(res => {
      Toast({
        context: this,
        selector: '#t-toast',
        message: '评价提交成功',
        icon: 'check-circle',
      });
      let pages = getCurrentPages();
      let beforePage = pages[pages.length - 2]; // -2 是上一个页面，-3是上上个页面以此类推。
      beforePage.restInit();
      wx.navigateBack({
        delta: 1,
      })
    })
  },
});
